package com.xjtu.se.homework.ops.faq.service.impl;

import com.xjtu.se.homework.common.Result;
import com.xjtu.se.homework.common.Utils;
import com.xjtu.se.homework.ops.faq.service.FaqService;
import com.xjtu.se.homework.ops.faq.dao.FaqDao;
import com.xjtu.se.homework.ops.faq.dto.FaqDto;
import com.xjtu.se.homework.ops.faq.dto.delDto;
import com.xjtu.se.homework.ops.faq.dto.pubDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;


@Service
@Transactional
public class FaqServiceImpl implements FaqService {
    @Resource
    private FaqDao faqDao;
    @Override
    public Map<String, Object> getAllFaq(FaqDto dto) {
        System.out.print("*****");
        Map<String, Object> page = Utils.getPage(dto, () -> faqDao.showAll(dto));
        return page;
    }

    @Override
    public Result faqUpd(FaqDto dto) {
        faqDao.faqUpd(dto);
        return Result.success("修改FAQ成功");
    }

    @Override
    public Result faqAdd(FaqDto dto) {
        try{
            System.out.print(dto.toString());
            faqDao.addFaq(dto);
            return Result.success("增加成功");
        }catch(Exception e){
            return Result.fail(500,"增加失败");
        }
    }

    @Override
    public Result faqDel(delDto dto) {
        try{
            System.out.print(dto.toString());
            faqDao.faqDel(dto);
            return Result.success("删除Faq成功");
        }catch(Exception e){
            return Result.fail(500,"删除Faq失败");
        }
    }

    @Override
    public Result faqPub(pubDto dto) {
        System.out.print(dto.toString());
        String status = dto.getStatus();
        if(status.equals("未发布")){
            dto.setStatus("已发布");
            faqDao.pubFaq(dto);
            return Result.success("发布成功");
        }else{
            dto.setStatus("未发布");
            faqDao.depubFaq(dto);
            return Result.success("下架成功");
        }
    }

//    @Override
//    public Map<String, Object> searchFaq(SearchDto dto) {
//        System.out.print(dto.toString());
//        Map<String, Object> page = Utils.getPage(dto, () -> faqDao.searchFaq(dto));
//        return page;
//    }
}
